package cn.hicard.risk.action.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hicard.risk.entity.SysDept;
import cn.hicard.risk.service.sys.DeptService;
import cn.hicard.util.DateHelp;

@Controller
@RequestMapping("/sys/dept")
public class DeptAction {
	
	@Autowired
	private DeptService deptService;
	
	@ResponseBody
	@RequestMapping
	public List<SysDept> list() throws Exception{
		return deptService.getAll();
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public void save(@RequestParam String name) throws Exception{
		SysDept dept = new SysDept();
		dept.setName(name);
		dept.setAdddatetime(DateHelp.dateTime());
		dept.setModdatetime("");
		deptService.save(dept);
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public void update(@RequestParam int pkDept, @RequestParam String name) throws Exception{
		SysDept dept = deptService.getByPk(pkDept);
		dept.setName(name);
		dept.setModdatetime(DateHelp.dateTime());
		deptService.update(dept);
	}

	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam int pkDept) throws Exception{
		// 检查是否有正在使用的，正在使用的不许删
		if(deptService.existsInUser(pkDept)){	
			return "{success:'0',msg:'不能删除已被使用的部门'}";
		}
		deptService.delete(pkDept);
		return "{success:'1'}";
	}
	
	@ResponseBody
	@RequestMapping("/detail")
	public SysDept detail(@RequestParam int pkDept) throws Exception{
		return deptService.getByPk(pkDept);
	}

}
